<style lang="less">
</style>

<template>
  <Card :bordered="false" :dis-hover="true">
    <p slot="title" style="height: 33px">
      <label style="margin-left: 20px">请输入会员编号和姓名：</label>
      <AutoComplete
        v-model="searchValue"
        placeholder="请输入会员编号和姓名"
        @keyup.enter.native="search"
        style="width:200px">
      </AutoComplete>
      <Button type="ghost" @click="recharge">充值</Button>
      <Button type="ghost" @click="gift">赠送</Button>
      <Button type="ghost" @click="openAccount">开户</Button>
    </p>
    <Row style="font-size: 16px; font-weight: bold; font-family: Helvetica, 'Hiragino Sans GB', 'Microsoft Yahei', '微软雅黑', Arial, sans-serif;">
      <Col :span="3" :offset="1">会员编号：</Col>
      <Col :span="3">会员姓名：<a href="#" style="font-weight: bold">{{member.memberName}}</a></Col>
      <Col :span="3">会员卡名：<a href="#" style="font-weight: bold">{{member.levelName}}{{member.level}}</a></Col>
      <Col :span="3">联系电话：<a href="#" style="font-weight: bold">{{patient.phone}}</a></Col>
      <Col :span="3">会员生日：<a href="#" style="font-weight: bold">{{patient.birthday}}</a></Col>
    </Row>
    <br/>
    <Row style="font-size: 16px; font-weight: bold; font-family: Helvetica, 'Hiragino Sans GB', 'Microsoft Yahei', '微软雅黑', Arial, sans-serif;">
      <Col :span="3" :offset="1">储值账户：<a href="#" style="font-weight: bold">{{member.storeAccount}}</a></Col>
      <Col :span="3">疗程账户：<a href="#" style="font-weight: bold">{{member.courseAccount | amountFilter}}</a></Col>
      <Col :span="3">赠送账户：<a href="#" style="font-weight: bold">{{member.giftAccount | amountFilter}}</a></Col>
      <Col :span="3">累计充值：<a href="#" style="font-weight: bold">{{member.totalStoreAmount | amountFilter}}</a></Col>
      <Col :span="3">累计赠送：<a href="#" style="font-weight: bold">{{member.totalGiftAmount | amountFilter}}</a></Col>
      <Col :span="3">累计消费：<a href="#" style="font-weight: bold">{{member.totalConsumeAmount | amountFilter}}</a></Col>
      <Col :span="3">会员积分：<a href="#" style="font-weight: bold">{{member.score}}</a></Col>
    </Row>
    <Tabs @on-click="clickTabs" :value="paneName">
      <TabPane name="1" label="会员缴费" icon="social-yen">
        <Card :bordered="false" :dis-hover="true">
          <p slot="title" style="height: 33px">
            <Button icon="plus-round" type="ghost" @click="addService">服务费</Button>
            <Button icon="loop" type="ghost" @click="discountMatch">折扣匹配</Button>
            <Button icon="close" type="ghost" @click="discountCancel">取消折扣</Button>
            <Button icon="plus-round" type="ghost">商品选购</Button>
          </p>
          <charge-list ref="list1" :patientId="patient.id" :data="chargeDetails" v-on="{compute:compute}"></charge-list>
        </Card>
        <Card :bordered="false" :dis-hover="true">
          <p slot="title">
            使用折扣
          </p>
          <label v-if="discounts.length === 0"><Icon style="color: #ff9900" type="information-circled"></Icon> 未使用折扣</label>
          <ul>
            <li v-for="item in discounts">
              <a href="#" style="font-weight: bold;font-size: 15px"><label style="margin-left: 30px">{{ item.discountName }}</label><label style="margin-left: 30px">{{ item.discount }}折</label></a>
            </li>
          </ul>
        </Card>
        <Card :bordered="false" :dis-hover="true">
          <p slot="title">
            服务费抵扣
          </p>
          <Row>
            <Col span="24">
              <service-project-list ref="serviceProjectList" :patientId="patient.id" v-on="{selectServiceProject:selectServiceProject}"></service-project-list>
            </Col>
          </Row>
        </Card>
        <Tabs>
          <TabPane name="1" label="联合付款">
            <Card :bordered="false" :dis-hover="true">
              <p slot="title">
                <Row>
                  <Col span="3">
                    <label style="font: bold 16px/20px arial, sans-serif; color: rgb(153, 153, 153);">总价格：<label style="color: red">{{payInfo.payAmount}}</label></label>
                  </Col>
                  <Col span="3">
                    <label style="font: bold 16px/20px arial, sans-serif; color: rgb(153, 153, 153);">实付金额：<label style="color: red">{{payInfo.actualAmount}}</label></label>
                  </Col>
                  <Col span="3">
                    <label style="font: bold 16px/20px arial, sans-serif; color: rgb(153, 153, 153);">已节省：<label style="color: red">- {{payInfo.discountAmount}}</label></label>
                  </Col>
                  <Col span="3">
                    <label style="font: bold 16px/20px arial, sans-serif; color: rgb(153, 153, 153);">抵扣金额：<label style="color: red">- {{payInfo.deductAmount}}</label></label>
                  </Col>
                </Row>
              </p>
              <Row>
                <Col :span="2">
                  <img style="width: 100px" src="@/images/pay/zhifu5.png"/>
                </Col>
                <Col :span="2">
                  <img style="width: 100px" src="@/images/pay/zhifu7.png"/>
                </Col>
                <Col :span="2">
                  <img style="width: 100px" src="@/images/pay/zhifu1.png"/>
                </Col>
                <Col :span="2">
                  <img style="width: 100px" src="@/images/pay/zhifu2.png"/>
                </Col>
                <Col :span="2">
                  <img style="width: 100px" src="@/images/pay/zhifu3.png"/>
                </Col>
                <Col :span="2">
                  <img style="width: 100px" src="@/images/pay/zhifu4.png"/>
                </Col>
              </Row>
              <Row>
                <Col :span="2">
                  <InputNumber style="width: 100px" v-model="payment.store" @on-change=""></InputNumber>
                </Col>
                <Col :span="2">
                  <InputNumber style="width: 100px" v-model="payment.gift"></InputNumber>
                </Col>
                <Col :span="2">
                  <InputNumber style="width: 100px" v-model="payment.alipay"></InputNumber>
                </Col>
                <Col :span="2">
                  <InputNumber style="width: 100px" v-model="payment.wechat"></InputNumber>
                </Col>
                <Col :span="2">
                  <InputNumber style="width: 100px" v-model="payment.bankCard"></InputNumber>
                </Col>
                <Col :span="2">
                  <InputNumber style="width: 100px" v-model="payment.cash"></InputNumber>
                </Col>
              </Row>
            </Card>
          </TabPane>
          <TabPane name="2" label="治疗卡">
            <Card :bordered="false" :dis-hover="true">
              <p slot="title" style="height: 33px">
                请输入卡号：<Input @keyup.enter.native="searchCode" v-model="cardNumber" style="width: 200px"></Input>
              </p>
              <Table border :columns="cardColumns" :data="cards"></Table>
            </Card>
          </TabPane>
        </Tabs>
        <Card :bordered="false" :dis-hover="true">
          <p slot="title">&nbsp;</p>
          <Row>
            <Col :span="2" :offset="22">
              <Button @click="doPayment" style="background-color: #f56954;width: 100%;height: 58px">
                <label style="color: #fff;font-size: 16px">付款</label>
              </Button>
            </Col>
          </Row>
        </Card>
      </TabPane>
      <TabPane name="2" label="会员划扣" icon="document-text">
        <Card :bordered="false" :dis-hover="true">
          <p slot="title" style="height: 33px">
            <Button type="ghost" @click="deduct">确认划扣</Button>
          </p>
          <Table border :columns="columns7" :data="data7"></Table>
        </Card>
      </TabPane>
      <TabPane name="3" label="欠费补交" icon="document-text">
        <wait-payment-list ref="list3" :patientId="patient.id" v-on="{refreshPatient:refreshPatient}"></wait-payment-list>
      </TabPane>
      <TabPane name="4" label="充值记录" icon="document-text">
        <Row :gutter="4">
          <Col :span="12">
            <Card :dis-hover="true">
              <recharge-record-list ref="list4" :patientId="patient.id" v-on="{clickRecharge:clickRecharge}"></recharge-record-list>
            </Card>
          </Col>
          <Col :span="12">
            <Card :dis-hover="true">
              <Card :bordered="false" :dis-hover="true">
                <p slot="title">
                  <Icon type="ios-film-outline"></Icon>
                  付款详情
                </p>
                <Table border :columns="columns1" :data="data1"></Table>
              </Card>
              <Card :bordered="false" :dis-hover="true">
                <p slot="title">
                  <Icon type="ios-film-outline"></Icon>
                  账户变化(充值前 > 充值后)
                </p>
                <Table border :columns="columns2" :data="data2"></Table>
              </Card>
            </Card>
          </Col>
        </Row>
      </TabPane>
      <TabPane name="5" label="赠送记录" icon="document-text">
        <gift-record-list ref="list5" :patientId="patient.id"></gift-record-list>
      </TabPane>
      <TabPane name="6" label="消费记录" icon="document-text">
        <Row :gutter="4">
          <Col :span="12">
            <Card :dis-hover="true">
              <consume-record-list ref="list6" :patientId="patient.id" v-on="{clickConsume:clickConsume}"></consume-record-list>
            </Card>
          </Col>
          <Col :span="12">
            <Card :dis-hover="true">
              <Card :bordered="false" :dis-hover="true">
                <p slot="title">
                  <Icon type="ios-film-outline"></Icon>
                  消费明细
                </p>
                <Table border :columns="columns6" :data="data6"></Table>
              </Card>
              <Card :bordered="false" :dis-hover="true">
                <p slot="title">
                  <Icon type="ios-film-outline"></Icon>
                  使用折扣
                </p>
                <Table border :columns="columns5" :data="data5"></Table>
              </Card>
              <Card :bordered="false" :dis-hover="true">
                <p slot="title">
                  <Icon type="ios-film-outline"></Icon>
                  付款详情
                </p>
                <Table border :columns="columns3" :data="data3"></Table>
              </Card>
              <Card :bordered="false" :dis-hover="true">
                <p slot="title">
                  <Icon type="ios-film-outline"></Icon>
                  账户变化(充值前 > 充值后)
                </p>
                <Table border :columns="columns4" :data="data4"></Table>
              </Card>
            </Card>
          </Col>
        </Row>
      </TabPane>
      <TabPane name="7" label="划扣记录" icon="document-text">
        <deduct-list ref="list7" :patientId="patient.id"></deduct-list>
      </TabPane>
      <TabPane name="8" label="补交记录" icon="document-text">
        <make-record-list ref="list8" :patientId="patient.id"></make-record-list>
      </TabPane>
      <TabPane name="9" label="抵扣记录" icon="document-text">
        <treat-plan-service-project-list ref="list9" :patientId="patient.id"></treat-plan-service-project-list>
      </TabPane>
    </Tabs>
    <Modal v-model="showService" :closable='false' :mask-closable=true :width="600">
      <h3 slot="header" style="color:#2D8CF0">添加服务费</h3>
      <service-list v-on="{selectService:selectService}"></service-list>
      <div slot="footer">
        <Button type="info" @click="doAddService">保存</Button>
      </div>
    </Modal>
    <Modal v-model="showPatient" :closable='false' :mask-closable=true :width="800">
      <h3 slot="header" style="color:#2D8CF0">会员开户</h3>
      <patient-list ref="patientList" v-on="{open:open}"></patient-list>
      <div slot="footer">
      </div>
    </Modal>
    <Modal v-model="showRecharge" :closable='false' :mask-closable=true :width="500">
      <h3 slot="header" style="color:#2D8CF0">会员充值</h3>
      <Form ref="formRecharge" :model="rechargeRecord" :label-width="100">
        <Row>
          <Col span="20" offset="2">
            <FormItem label="充值金额：">
              <InputNumber size="large" style="width: 100%" :min="1" v-model="rechargeRecord.rechargeAmount"></InputNumber>
            </FormItem>
            <FormItem label="支付宝付款：">
              <InputNumber size="large" style="width: 100%" :min="0" v-model="paymentDetails.alipay"></InputNumber>
            </FormItem>
            <FormItem label="微信付款：">
              <InputNumber size="large" style="width: 100%" :min="0" v-model="paymentDetails.wechat"></InputNumber>
            </FormItem>
            <FormItem label="银联付款：">
              <InputNumber size="large" style="width: 100%" :min="0" v-model="paymentDetails.bankCard"></InputNumber>
            </FormItem>
            <FormItem label="现金付款：">
              <InputNumber size="large" style="width: 100%" :min="0" v-model="paymentDetails.cash"></InputNumber>
            </FormItem>
          </Col>
        </Row>
      </Form>
      <div slot="footer">
        <Button type="info" @click="doRecharge">充值</Button>
      </div>
    </Modal>
    <Modal v-model="showGift" :closable='false' :mask-closable=true :width="500">
      <h3 slot="header" style="color:#2D8CF0">会员赠送</h3>
      <Form ref="formGift" :model="giftRecord" :label-width="80">
        <Row>
          <Col span="20" offset="2">
            <FormItem label="赠送金额：">
              <InputNumber v-model="giftRecord.giftAmount" size="large" style="width: 100%" :min="1"></InputNumber>
            </FormItem>
            <FormItem label="赠送备注：">
              <Input v-model="giftRecord.giftRemark" type="textarea" :autosize="{minRows: 2,maxRows: 5}"></Input>
            </FormItem>
          </Col>
        </Row>
      </Form>
      <div slot="footer">
        <Button type="info" @click="doGift">赠送</Button>
      </div>
    </Modal>
  </Card>
</template>

<script>
    import chargeList from '@/view/finance/list/ChargeDetailsList.vue';
    import serviceList from '@/view/finance/list/ServiceList.vue';
    import patientList from '@/view/finance/list/PatientList.vue';
    import rechargeRecordList from '@/view/finance/list/RechargeRecordList.vue';
    import giftRecordList from '@/view/finance/list/GiftRecordList.vue';
    import consumeRecordList from '@/view/finance/list/ConsumeRecordList.vue';
    import deductList from '@/view/finance/list/DeductList.vue';
    import waitPaymentList from '@/view/finance/list/WaitPaymentList.vue';
    import makeRecordList from '@/view/finance/list/MakeRecordList.vue';
    import serviceProjectList from '@/view/finance/list/ServiceProjectList.vue';
    import treatPlanServiceProjectList from '@/view/finance/list/TreatPlanServiceProjectList.vue';
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    import Table from "../common/table/Table";
    export default {
        components: {
            Table,
            iviewTable,
            iviewPage,
            chargeList,
            serviceList,
            patientList,
            rechargeRecordList,
            giftRecordList,
            consumeRecordList,
            deductList,
            waitPaymentList,
            makeRecordList,
            serviceProjectList,
            treatPlanServiceProjectList
        },
        data () {
            return {
                chargeURL: '/user/charge',
                memberURL: '/user/member',
                patientURL: '/user/patient',
                deductURL: '/user/deduct',
                showService: false,
                showPatient: false,
                showRecharge: false,
                showGift: false,
                serviceProjects: [],
                chargeDetails: [],
                discounts: [],
                serviceIds: [],
                member: {},
                patient: {},
                searchValue: '',
                cardNumber: '',
                cards: [],
                cardColumns: [
                    {
                        title: '卡号',
                        key: 'code'
                    },
                    {
                        title: '疗程名称',
                        key: 'goodsName'
                    },
                    {
                        title: '价格',
                        key: 'retailPrice'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'default'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.index, params.row)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                rechargeRecord: {
                    id: '',
                    patientId: '',
                    patientName: '',
                    memberNumber: '',
                    rechargeAmount: ''
                },
                giftRecord: {
                    id: '',
                    patientId: '',
                    patientName: '',
                    memberId: '',
                    memberNumber: '',
                    giftAmount: '',
                    giftRemark: ''
                },
                paymentDetails: {
                    bankCard: '',
                    alipay: '',
                    wechat: '',
                    cash: ''
                },
                payment: {
                    store: '',
                    gift: '',
                    bankCard: '',
                    alipay: '',
                    wechat: '',
                    cash: ''
                },
                payInfo: {
                    payAmount: '0.00',
                    discountAmount: '0.00',
                    deductAmount: '0.00',
                    actualAmount: '0.00'
                },
                paneName: '1',
                recordId: '',
                data1: [],
                columns1: [
                    {
                        title: '支付宝',
                        key: 'alipay',
                        render: (h, params) => {
                            let temp = params.row.alipay
                            if (temp === undefined || temp === null) {
                                temp = '未使用'
                            }
                            return temp
                        }
                    },
                    {
                        title: '微信',
                        key: 'wechat',
                        render: (h, params) => {
                            let temp = params.row.wechat
                            if (temp === undefined || temp === null) {
                                temp = '未使用'
                            }
                            return temp
                        }
                    },
                    {
                        title: '银行卡',
                        key: 'bankCard',
                        render: (h, params) => {
                            let temp = params.row.bankCard
                            if (temp === undefined || temp === null) {
                                temp = '未使用'
                            }
                            return temp
                        }
                    },
                    {
                        title: '现金',
                        key: 'cash',
                        render: (h, params) => {
                            let temp = params.row.cash
                            if (temp === undefined || temp === null) {
                                temp = '未使用'
                            }
                            return temp
                        }
                    }
                ],
                data2: [],
                columns2: [
                    {
                        title: '储值账户',
                        render: (h, params) => {
                            return params.row.beforeStoreAccount + ' > ' + params.row.afterStoreAccount

                        }
                    },
                    {
                        title: '会员累计金额',
                        render: (h, params) => {
                            return params.row.beforeMemberAmount + ' > ' + params.row.afterMemberAmount
                        }
                    },
                    {
                        title: '会员等级',
                        render: (h, params) => {
                            return params.row.beforeLevelName + ' > ' + params.row.afterLevelName
                        }
                    }
                ],
                data3: [],
                columns3: [
                    {
                        title: '余额',
                        key: 'store',
                        render: (h, params) => {
                            let temp = params.row.store
                            if (temp === undefined || temp === null) {
                                temp = '未使用'
                            }
                            return temp
                        }
                    },
                    {
                        title: '赠送账户',
                        key: 'gift',
                        render: (h, params) => {
                            let temp = params.row.gift
                            if (temp === undefined || temp === null) {
                                temp = '未使用'
                            }
                            return temp
                        }
                    },
                    {
                        title: '支付宝',
                        key: 'alipay',
                        render: (h, params) => {
                            let temp = params.row.alipay
                            if (temp === undefined || temp === null) {
                                temp = '未使用'
                            }
                            return temp
                        }
                    },
                    {
                        title: '微信',
                        key: 'wechat',
                        render: (h, params) => {
                            let temp = params.row.wechat
                            if (temp === undefined || temp === null) {
                                temp = '未使用'
                            }
                            return temp
                        }
                    },
                    {
                        title: '银行卡',
                        key: 'bankCard',
                        render: (h, params) => {
                            let temp = params.row.bankCard
                            if (temp === undefined || temp === null) {
                                temp = '未使用'
                            }
                            return temp
                        }
                    },
                    {
                        title: '现金',
                        key: 'cash',
                        render: (h, params) => {
                            let temp = params.row.cash
                            if (temp === undefined || temp === null) {
                                temp = '未使用'
                            }
                            return temp
                        }
                    }
                ],
                data4: [],
                columns4: [
                    {
                        title: '余额',
                        render: (h, params) => {
                            return params.row.beforeStoreAccount + ' > ' + params.row.afterStoreAccount
                        }
                    },
                    {
                        title: '赠送账户',
                        render: (h, params) => {
                            return params.row.beforeGiftAccount + ' > ' + params.row.afterGiftAccount
                        }
                    },
                    {
                        title: '疗程账户',
                        render: (h, params) => {
                            return params.row.beforeCourseAccount + ' > ' + params.row.afterCourseAccount
                        }
                    }
                ],
                data5: [],
                columns5: [
                    {
                        title: '折扣名称',
                        key: 'discountName'
                    },
                    {
                        title: '折扣率',
                        key: 'discount'
                    }
                ],
                data6: [],
                columns6: [
                    {
                        title: '购买商品',
                        key: 'itemName'
                    },
                    {
                        title: '数量',
                        key: 'quantity'
                    },
                    {
                        title: '应付金额',
                        key: 'payAmount'
                    },
                    {
                        title: '优惠金额',
                        key: 'discountAmount'
                    },
                    {
                        title: '实付金额',
                        key: 'actualAmount'
                    }
                ],
                data7: [],
                columns7: [
                    {
                        title: '项目来源',
                        key: 'treatName'
                    },{
                        title: '项目名称',
                        key: 'itemName'
                    },{
                        title: '单次划扣金额',
                        key: 'price'
                    },{
                        title: '总划扣(次数/数量)',
                        key: 'totalDeductCount'
                    },{
                        title: '已划扣(次数/数量)',
                        key: 'tickedCount'
                    },{
                        title: '本次划扣(次数/数量)',
                        key: 'deductCount',
                        render: (h, params) => {
                            var vm = this
                            return h('InputNumber',{
                                props: {
                                    min: 1,
                                    max: params.row.totalDeductCount - params.row.tickedCount,
                                    value: params.row.deductCount
                                },
                                style: {
                                    width: '100%'
                                },
                                on: {
                                    'on-change' (value) {
                                        params.row.deductCount = value
                                        params.row.deductAmount = params.row.price * params.row.deductCount
                                        vm.data7[params.index] = params.row
                                    }
                                }
                            });
                        }
                    },{
                        title: '本次划扣金额',
                        key: 'deductAmount'
                    }
                ]
            };
        },
        filters: {
            amountFilter (value) {
                let temp = value
                if (value === undefined || value === null) {
                    temp = '0.00'
                }
                return temp
            },
            birthdayFilter (value) {

            }
        },
        methods: {
            clickTabs (name) {
                if (name === '1') {
                    // 刷新待付款
                    this.refreshCharge()
                    // 刷新服务项目
                    this.refreshServiceProject()
                } else if (name === '2') {
                    this.searchDeduct()
                } else if (name === '3') {
                    // 查询欠费列表
                    this.refreshWaitPayment()
                    // 重置付款信息
                    this.$refs.list3.resetPayInfo()
                } else if (name === '4') {
                    this.data1 = []
                    this.data2 = []
                    this.refreshRechargeRecord()
                } else if (name === '5') {
                    this.refreshGiftRecord()
                } else if (name === '6') {
                    this.data1 = []
                    this.data2 = []
                    this.refreshConsumeRecord()
                } else if (name === '7') {
                    this.refreshDeductRecord()
                } else if (name === '8') {
                    this.refreshMakeRecord()
                } else if (name === '9') {
                    this.refreshTreatService()
                }
                this.paneName = name
            },
            clickRecharge (row) {
                if (this.paneName === '4') {
                    let url = '/user/payment/details/find/' + row.id
                    this.$ajax.get(url).then((response) => {
                        let {code, data, msg} = response.data
                        if (code === 0) {
                            this.data1 = data
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                    url = '/user/account/change/details/find/' + row.id
                    this.$ajax.get(url).then((response) => {
                        let {code, data, msg} = response.data
                        if (code === 0) {
                            this.data2 = data
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                }
            },
            clickConsume (row) {
                if (this.paneName === '6') {
                    let url = '/user/payment/details/find/' + row.id
                    this.$ajax.get(url).then((response) => {
                        let {code, data, msg} = response.data
                        if (code === 0) {
                            this.data3 = data
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                    url = '/user/account/change/details/find/' + row.id
                    this.$ajax.get(url).then((response) => {
                        let {code, data, msg} = response.data
                        if (code === 0) {
                            this.data4 = data
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                    url = "/user/consume/discount/find/" + row.id
                    this.$ajax.get(url).then((response) => {
                        let {code, data, msg} = response.data
                        if (code === 0) {
                            this.data5 = data
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                    url = '/user/consume/details/record/find/' + row.id
                    this.$ajax.get(url).then((response) => {
                        let {code, data, msg} = response.data
                        if (code === 0) {
                            this.data6 = data
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                }
            },
            search () {
                if (this.searchValue !== '') {
                    let url = this.memberURL + '/find/' + this.searchValue
                    this.$ajax.get(url).then((response) => {
                        let {code, data, msg} = response.data
                        if (code === 0) {
                            if (data.length === 1) {
                                this.member = data[0]
                                data[0].patient.birthday = this.$moment(data[0].patient.birthday).format('YYYY年MM月DD日')
                                this.patient.id = ''
                                this.patient = data[0].patient
                                if (this.paneName === '1') {
                                    this.refreshCharge()
                                    this.refreshServiceProject()
                                } else if (this.paneName === '2') {
                                    this.refreshDeduct()
                                }
                            } else if (data.length > 1) {
                                this.$bus.alert('warning', '检索到多个患者信息，请点击右侧搜索按钮')
                            } else {
                                this.$bus.alert('warning', '没有检索到患者信息')
                            }
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '请键入搜索词')
                }
            },
            searchCode () {
                if (this.cardNumber.trim() !== '') {
                    let url = '/wx/order/goods/code/findByCode?code=' + this.cardNumber.trim()
                    this.$app.get(url).then((response) => {
                        let {errno, data, errmsg} = response.data
                        if (errno === 0) {
                            this.cards.push(data)
                        } else {
                            this.$bus.alert('error', errmsg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '请输入卡号');
                }
            },
            remove (index, row) {
                this.cards.splice(index, 1);
            },
            searchDeduct () {
                let url = this.deductURL + '/findByPatientId/' + this.patient.id
                this.$ajax.get(url).then((response) => {
                    let {code, data, msg} = response.data
                    if (code === 0) {
                        this.data7 = data
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            // 选择服务项目
            selectService (rows) {
                this.serviceProjects = rows
            },
            selectServiceProject (rows) {
                let deductAmount = 0
                this.serviceIds = []
                for (let row of rows) {
                    deductAmount += parseFloat(row.price)
                    this.serviceIds.push(row.id)
                }
                this.payInfo.deductAmount = deductAmount
                if (this.chargeDetails.length > 0) {
                    this.payInfo.actualAmount = this.payInfo.actualAmount - deductAmount
                }
            },
            // 添加服务项目
            addService () {
                if (this.member.hasOwnProperty('id') && this.member.id !== '') {
                    this.showService = true
                } else {
                    this.$bus.alert('warning', '请先确定购买会员')
                }
            },
            // 保存服务项目
            doAddService () {
                if (this.serviceProjects.length > 0) {
                    let url = this.memberURL + '/addService/' + this.patient.id + '/' + this.member.id
                    this.$ajax.post(url, this.serviceProjects).then((response) => {
                        let {code, msg} = response.data
                        if (code === 0) {
                            this.refreshCharge()
                            this.showService = false
                            this.$bus.alert('success', '添加成功')
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                }
            },
            recharge () {
                if (this.member.hasOwnProperty('id') && this.member.id !== '') {
                    this.$bus.init(this.rechargeRecord)
                    this.$bus.init(this.paymentDetails)
                    this.showRecharge = true
                } else {
                    this.$bus.alert('warning', '请先确定购买会员')
                }
            },
            doRecharge () {
                // 基本校验
                if (this.rechargeRecord.rechargeAmount === '') {
                    this.$bus.alert('warning', '请输入充值金额')
                    return;
                }
                if (this.paymentDetails.alipay === '' && this.paymentDetails.bankCard === ''
                    && this.paymentDetails.cash === '' && this.paymentDetails.wechat === '') {
                    this.$bus.alert('warning', '请选择付款方式并正确输入金额')
                    return;
                }
                let price = 0
                if (this.paymentDetails.alipay !== '') {
                    price += parseFloat(this.paymentDetails.alipay)
                }
                if (this.paymentDetails.bankCard !== '') {
                    price += parseFloat(this.paymentDetails.bankCard)
                }
                if (this.paymentDetails.cash !== '') {
                    price += parseFloat(this.paymentDetails.cash)
                }
                if (this.paymentDetails.wechat !== '') {
                    price += parseFloat(this.paymentDetails.wechat)
                }
                let rechargeAmount = parseFloat(this.rechargeRecord.rechargeAmount)
                if (price !== rechargeAmount) {
                    this.$bus.alert('warning', '请正确输入付款金额')
                    return
                }
                // 提交后台处理
                this.rechargeRecord.patientId = this.patient.id
                this.rechargeRecord.memberId = this.member.id
                this.rechargeRecord.memberName = this.patient.name
                this.rechargeRecord.memberNumber = this.member.memberNumber
                this.rechargeRecord.paymentDetails = this.paymentDetails
                let url = this.memberURL + '/recharge'
                this.$ajax.post(url, this.rechargeRecord).then((response) => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.showRecharge = false
                        this.$bus.modal('success', '系统提示', '充值成功')
                        this.search()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            gift () {
                if (this.member.hasOwnProperty('id') && this.member.id !== '') {
                    this.$bus.init(this.giftRecord)
                    this.showGift = true
                } else {
                    this.$bus.alert('warning', '请先确定购买会员')
                }
            },
            doGift () {
                if (this.giftRecord.giftAmount === '') {
                    this.$bus.alert('warning', '请输入赠送金额')
                    return;
                }
                this.giftRecord.patientId = this.patient.id
                this.giftRecord.memberId = this.member.id
                this.giftRecord.memberName = this.patient.name
                this.giftRecord.memberNumber = this.member.memberNumber
                let url = this.memberURL + '/gift'
                this.$ajax.post(url, this.giftRecord).then((response) => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.showGift = false
                        this.$bus.modal('success', '系统提示', '赠送成功')
                        this.search()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            compute (data) {
                if (data.length > 0) {
                    let url = this.chargeURL + '/compute'
                    this.$ajax.post(url, data).then((response) => {
                        let {code, data, msg} = response.data
                        if (code === 0) {
                            this.payInfo = data
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                    this.chargeDetails = data
                } else {
                    this.payInfo.payAmount = '0.00'
                    this.payInfo.discountAmount = '0.00'
                    this.payInfo.deductAmount = '0.00'
                    this.payInfo.actualAmount = '0.00'
                }
            },
            doPayment () {
               if (this.chargeDetails.length > 0) {
                  let url = this.memberURL + '/payment'
                  this.$ajax.post(url, {
                      chargeDetails: JSON.stringify(this.chargeDetails),
                      discounts: JSON.stringify(this.discounts),
                      payInfo: JSON.stringify(this.payInfo),
                      payment: JSON.stringify(this.payment),
                      serviceIds: JSON.stringify(this.serviceIds)
                  }).then((response) => {
                      let {code, msg} = response.data
                      if (code === 0) {
                          this.$bus.alert('success', msg)
                          this.$bus.init(this.payment)
                          this.search()
                      } else {
                          this.$bus.alert('error', msg)
                      }
                  })
               } else {
                   this.$bus.alert('warning', '没有待付款商品')
               }
            },
            deduct () {
                if (this.data7.length > 0) {
                    let url = this.memberURL + '/deduct'
                    this.$ajax.post(url, this.data7).then((response) => {
                        let {code, msg} = response.data
                        if (code === 0) {
                            this.$bus.alert('success', '操作成功')
                            this.search()
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '没有待划扣项目')
                }
            },
            openAccount () {
                this.$refs.patientList.refresh()
                this.showPatient = true
            },
            open (row) {
                let url = this.memberURL + '/openAccount'
                this.$ajax.post(url, row).then((response) => {
                    let {code, data, msg} = response.data
                    if (code === 0) {
                        this.showPatient = false
                        this.$bus.modal('success', '系统提示', '开户成功')
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            discountMatch () {
                if (this.chargeDetails.length > 0) {
                    let url = this.chargeURL + '/matchDiscount'
                    this.$ajax.post(url, this.chargeDetails).then((response) => {
                        let {code, data, msg} = response.data
                        if (code === 0) {
                            this.chargeDetails = data.data
                            this.discounts = data.discount
                            url = this.chargeURL + '/compute'
                            this.$ajax.post(url, this.chargeDetails).then((response) => {
                                let {code, data, msg} = response.data
                                if (code === 0) {
                                    this.payInfo = data
                                } else {
                                    this.$bus.alert('error', msg)
                                }
                            })
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '没有待付款商品')
                }
            },
            discountCancel () {
                this.discounts = []
                this.search()
            },
            refreshPatient() {
                this.search()
            },
            refreshCharge () {
                this.$refs.list1.refresh()
            },
            refreshServiceProject () {
                this.$refs.serviceProjectList.refresh()
            },
            refreshWaitPayment () {
                this.$refs.list3.refresh()
            },
            refreshRechargeRecord () {
                this.$refs.list4.refresh()
            },
            refreshGiftRecord () {
                this.$refs.list5.refresh()
            },
            refreshConsumeRecord () {
                this.$refs.list6.refresh()
            },
            refreshDeduct () {
                this.searchDeduct()
            },
            refreshDeductRecord () {
                this.$refs.list7.refresh()
            },
            refreshMakeRecord () {
                this.$refs.list8.refresh()
            },
            refreshTreatService () {
                this.$refs.list9.refresh()
            }
        },
        watch: {
            '$route' (to) {
                if (to.path === '/center') {
                    this.refreshCharge()
                }
            }
        }
    }
</script>

<style>

</style>
